package mhcs.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import javax.swing.Popup;
import java.awt.Button;

import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalSplitPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;


class LoginPopup extends Composite {
	
	//Boolean to determine if successful login was achieved.
	private boolean success = false;
	
	public boolean getSuccess(){
		return success;
	}

	
	
	public LoginPopup() {
		
		//Create the information needed to check to see if username and password match
		final LoginInformation loginInformation = new LoginInformation();
			
		final Label invalidText = new Label("Invalid username or password.");
		invalidText.setStyleName("gwt-InvalidLogin-Label");
		invalidText.setVisible(false);
		
		final PasswordTextBox passwordTextBox = new PasswordTextBox();
		
		FlexTable flexTableWrapper = new FlexTable();
		flexTableWrapper.setStyleName("gwt-FlexTable-Wrapper");
		initWidget(flexTableWrapper);
		flexTableWrapper.setSize("1100px", "550px");
		
		Label lblMarsHabitatConfigration = new Label("Mars Habitat Configuration System");
		lblMarsHabitatConfigration.setStyleName("gwt-Main-Title");
		flexTableWrapper.setWidget(0, 0, lblMarsHabitatConfigration);
		
		FlexTable flexTableWidget = new FlexTable();
		flexTableWrapper.setWidget(1, 0, flexTableWidget);
		flexTableWidget.setSize("400px", "175px");
		
		Label label = new Label("Log in to the MHCS");
		label.setStyleName("gwt-MainMenu-Labels");
		flexTableWidget.setWidget(0, 0, label);
		
		Label label_1 = new Label("Username: ");
		label_1.setStyleName("gwt-MainMenu-Labels");
		flexTableWidget.setWidget(2, 0, label_1);
		label_1.setWidth("112px");
		
		final TextBox textBoxUsername = new TextBox();
		flexTableWidget.setWidget(2, 1, textBoxUsername);
		textBoxUsername.setSize("150px", "20px");
		
		Label label_2 = new Label("Password:");
		label_2.setStyleName("gwt-MainMenu-Labels");
		flexTableWidget.setWidget(3, 0, label_2);
		label_2.setHeight("32px");
		
		com.google.gwt.user.client.ui.Button loginButton = new com.google.gwt.user.client.ui.Button("Login");
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if((textBoxUsername.getText().equals(loginInformation.getUsername())) && 
						passwordTextBox.getText().equals(loginInformation.getPassword())){

					//MHCS.soundOutput.actionPerformed("login", new Module(0, "", "", 0, 0, 0));
					MHCS.soundOutput.playSound3();
					
					RootPanel.get().clear();

				    MainMenu mainMenu = new MainMenu();
				    RootPanel.get().add(mainMenu);
				    
				} else {
					invalidText.setVisible(true);
				}
			}
		});
		
		
		flexTableWidget.setWidget(3, 1, passwordTextBox);
		passwordTextBox.setSize("150px", "20px");
		flexTableWidget.setWidget(4, 0, loginButton);
		
		com.google.gwt.user.client.ui.Button resetButton = new com.google.gwt.user.client.ui.Button("Reset ");
		resetButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				textBoxUsername.setText("");
				passwordTextBox.setText("");
				invalidText.setVisible(false);
				
			}
		});
		flexTableWidget.setWidget(4, 1, resetButton);
		flexTableWidget.getCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTableWidget.getCellFormatter().setHorizontalAlignment(4, 2, HasHorizontalAlignment.ALIGN_LEFT);
		flexTableWidget.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableWidget.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableWidget.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableWrapper.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableWrapper.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		
		
		flexTableWrapper.setWidget(2, 0, invalidText);
		flexTableWrapper.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableWrapper.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
	
	}


}
